﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MapEntities;
using ServerPlatform.Web;
using ServerPlatform.Exceptions;
using DALEntities;

namespace WebApp.Controls
{
    public partial class SymbolDefinitonsEditor : BaseDataEditor<SymbolItemVisualization>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (string xsymboltype in Enum.GetNames(typeof(SymbolType)))
                {
                    ctlSymbolType.Items.Add(new ListItem(xsymboltype, xsymboltype));
                    ctlDefaultSymbolType.Items.Add(new ListItem(xsymboltype, xsymboltype));
                }
                foreach (string xdatatype in Enum.GetNames(typeof(DataType)))
                {
                    ctlRangeDataType.Items.Add(new ListItem(xdatatype, xdatatype));
                    
                }


            }
        }

        protected void ShowSelectedEntity(int xindex, SymbolItemVisualization entity)
        {
            SymbolItemRange xitem = entity.RangeList[xindex];
            ctlRangeStart.Text = xitem.Start.ToString();
            ctlRangeEnd.Text = xitem.Finish.ToString();
            ctlSymbolType.SelectedIndex = ctlSymbolType.Items.IndexOf(ctlSymbolType.Items.FindByText(xitem.Symbol.ToString()));
            ctlRangeDataType.SelectedIndex = ctlRangeDataType.Items.IndexOf(ctlRangeDataType.Items.FindByText(entity.RangeList.RangeDataType.ToString()));
            ctlDefaultSymbolType.SelectedIndex = ctlDefaultSymbolType.Items.IndexOf(ctlDefaultSymbolType.Items.FindByText(entity.DefaultSymbolType.ToString()));
        }

        protected override void ShowEntity(SymbolItemVisualization entity)
        {
            ctlRangeList.Items.Clear();
            if (entity.RangeList.Count > 0)
            {
                foreach (SymbolItemRange xitem in entity.RangeList.List)
                {
                    ctlRangeList.Items.Add(xitem.Start + " - " + xitem.Finish);
                }
                ctlRangeList.SelectedIndex = 0;
                ShowSelectedEntity(0, entity);                

            }
            else
            {
                ctlRangeStart.Text = string.Empty;
                ctlRangeEnd.Text = string.Empty;
                ctlSymbolType.SelectedIndex = 0;                
            }

        }

        public override SymbolItemVisualization GetEntity()
        {
            Current.RangeList.RangeDataType = (DataType)Enum.Parse(typeof(DataType), ctlRangeDataType.SelectedItem.Text);
            try
            {
                Current.RangeList.ConvertRanges();
            }
            catch (Exception ex)
            {
                throw new BusinessException(ex.Message.ToString());
            }
            Current.DefaultSymbolType = (SymbolType)Enum.Parse(typeof(SymbolType), ctlDefaultSymbolType.SelectedItem.Text);            
            SaveEntity();
            return Current;
        }

        protected void ctlRangeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowSelectedEntity(ctlRangeList.SelectedIndex, Current);
        }

        public override void ClearEditor()
        {
            ctlDefaultSymbolType.SelectedIndex = 0;
            ctlRangeEnd.Text = string.Empty;
            ctlRangeStart.Text = string.Empty;
            ctlSymbolType.SelectedIndex = 0;
            ctlRangeDataType.SelectedIndex = 0;
            
        }

        protected void ctlClear_Click(object sender, EventArgs e)
        {
            ClearEditor();
            ctlRangeList.Items.Clear();
            ClearCurrent();
        }

        protected void ctlRemoveRange_Click(object sender, EventArgs e)
        {
            int xindex = ctlRangeList.SelectedIndex;
            if (xindex < 0)
            {
                BusinessException xex = new BusinessException("You must select a range to delete");
                throw xex;
            }
            Current.RangeList.RemoveAt(xindex);
            ctlRangeList.Items.RemoveAt(xindex);
            SaveEntity();
            ClearEditor();
        }

        protected void SaveItem(SymbolItemRange xitem)
        {
            xitem.Start = ctlRangeStart.Text.Trim();
            xitem.Finish = ctlRangeEnd.Text.Trim();
            xitem.Symbol = (SymbolType)Enum.Parse(typeof(SymbolType), ctlSymbolType.SelectedItem.Text);
        }

        protected void ctlAddRange_Click(object sender, EventArgs e)
        {
            SymbolItemRange xitem = new SymbolItemRange();
            SaveItem(xitem);
            Current.RangeList.Add(xitem);
            SaveEntity();
            ctlRangeList.Items.Add(xitem.Start + " - " + xitem.Finish);
            ctlRangeList.SelectedIndex = ctlRangeList.Items.Count - 1;
        }

        protected void ctlUpdateRange_Click(object sender, EventArgs e)
        {
            int xindex = ctlRangeList.SelectedIndex;
            if (xindex < 0)
            {
                BusinessException xex = new BusinessException("You must select a range to update");
                throw xex;
            }
            SymbolItemRange xitem = Current.RangeList[xindex];
            SaveItem(xitem);
            Current.RangeList[xindex] = xitem;
            SaveEntity();
            ShowEntity();
        }


    }
}